#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>

int main ()
{
    
    /*
     * 测试进程的替换
     */
    if (fork() == 0) {
        // child
        
        execl("/usr/bin/ls", "ls", "-a", "-l", "-n", NULL);
        
        printf("hello world\n");
        printf("hello world\n");
        printf("hello world\n");
        printf("hello world\n");
        printf("hello world\n");
        printf("hello world\n");
    }
    // parent
    int cnt  = 3;
    while (cnt) {
        printf ("i am parent \n");
        sleep(1);
        --cnt;
    }
    waitpid(-1, NULL, 0);
    printf("wait success\n");





    //pid_t id = fork();
   /* 
    * 测试pid_t wait(int status);函数
    * 子进程创建成功，并exit(10);进行了进程终止，此时ret以特殊的格式来接收到该值
    if(id == 0) {
        // child
        // 测试异常终止
        //int x = 10;
        //x /= 0;

        int count = 3;
        while (count) {
            printf("I am child  count = %d  pid : %d\n", count, getpid());
            sleep(1);
             count--;
        }
        exit(0);
    }
    int status = 0;
    // waitpid(pid_t pid, int *status, int options);
    // pid : 进程创建的返回值
    // *status ：输出型参数， 后16为将保存进程的一些信息
    // 正常状态（程序正常结束，结果是否正确就看退出状态/退出码）
    // 15           8 7         0
    //  退出状态        0
    // 被信号所杀（异常终止）
    // 15           8   7        6       0
    //  退出状态      core dump    终止信号
    pid_t ret = waitpid(id, &status, 0);
    printf("ret = %d  status = %d exit_code = %d  exit_singal = %d\n",ret, status, (status >> 8) & 0xff, status & 0x7f);
    // exit_code的宏命令：WIFEXITED(status)
    // exit_singal的宏命令：WEXITSTATUS(status)
    //printf("ret = %d  exit_code = %d  exit_singal = %d\n", ret, WIFEXITED(status), WEXITSTATUS(status));
    
    /*
    int ex = 5;
    while (ex) {
        pid_t ret = waitpid(id, &status, WNOHANG);
        printf("ret = %d  exit_code = %d  exit_singal = %d\n", ret, WIFEXITED(status), WEXITSTATUS(status));
        --ex;
        sleep(1);
    }
    /**/

   /* */
   
    /* 测试wait函数的参数，返回值
     *
     *
    if (id == 0) {
        // child
        
        int cnt = 3;
        while (cnt) {
            printf ("i am child cnt = %d pid = %d\n", cnt, getpid());
            --cnt;
            sleep(1);
        }
        exit(1);
    }
    // parent
   
    int status = 0;
    int ret = wait(&status);
    while(1) {
        printf ("i am parent  ret = %d  status = %d\n", ret, status);
        sleep(1);
    }
    */

    /*
     * 测试printf的刷新
        printf("123123");
        sleep(3);

        _exit(EXIT_SUCCESS);
    */


   /* 
    * 测试pid_t wait(int status);函数
    * 子进程创建成功，并exit(10);进行了进程终止，此时ret以特殊的格式来接收到该值
    if(id == 0) {
        // child
        
        // 测试异常终止
        int x = 10;
        x /= 0;

        int count = 3;
        while (count) {
            printf("I am child  count = %d  pid : %d\n", count, getpid());
            sleep(1);
             count--;
        }
        exit(10);
    }
    pid_t ret = wait(NULL);
    if(ret == 0) {
        printf("child success ret : %d\n", ret);
    }
    else {
        printf("child failed ret : %d\n", ret);
    }
    */

    /*
     * 正常的创建进程 子进程在执行了5秒之后，转为僵尸状态，然后父进程再等待3秒，进程结束
    int x = 50;
    if(id == 0) {
        // child
        
        int count = 5;
        while (count) {
            if (count == 3)
                x = 100;
            printf("I am child  count = %d  pid : %d  x  =  %d\n", count, getpid(), x);
            sleep(1);
            count--;
        }
    }
    sleep(6);
    printf("i am parent  x  =  %d\n", x);
    int i = 0;
    for (i = 0; i < 140; i++) {
        printf("strerror[%d] : %s\n", i, strerror(i));
    }

    */


    return 12;
}
